package cn.kjcoder.redis.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2022/1/21
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@Service
@Slf4j
public class InitCrowdService {

    @Autowired
    private RedisTemplate redisTemplate;
    public static final String CROWDS_KEY = "change:crowds";

    @PostConstruct
    public void init(){
        log.info("数据初始化:群");
        List<String> crowds = this.crowds();
        this.redisTemplate.delete(CROWDS_KEY);
        crowds.forEach(t -> this.redisTemplate.opsForSet().add(CROWDS_KEY,t));
    }

    /**
     * 模拟100个群，用于推荐
     * @return
     */
    public List<String> crowds() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Random random = new Random();
            int id = random.nextInt(100);
            list.add("群" + id);
        }
        return list;
    }
}
